package com.thundersoft.musicplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.thundersoft.musicplayer.R;
import com.thundersoft.musicplayer.pojo.Music;

import java.util.List;

/**
 * @Author: liuzh0705
 * @Date:2021/07/27/下午5:37
 * @Description:
 */
public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MusicHodler> {

    private Context mContext;
    private List<Music> musicList;

    public MusicAdapter(Context mContext, List<Music> musicList) {
        this.mContext = mContext;
        this.musicList = musicList;
    }

    @Override
    public MusicHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item_layout, parent, false);
        MusicHodler hodler = new MusicHodler(view);

        return hodler;
    }

    /**
     * 为每个RecylerView子项赋值
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(MusicAdapter.MusicHodler holder, int position) {
        Music music = musicList.get(position);
        holder.tv_music_name.setText(music.getMusicName());
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }


    static class MusicHodler extends RecyclerView.ViewHolder {

        TextView tv_music_name;

        public MusicHodler(View view) {
            super(view);
            tv_music_name = view.findViewById(R.id.tv_music_name);
        }
    }
}
